using Digitalmes.Application.Mes.Productions.Products;
using Digitalmes.Domain.Entities.Mes.Productions;

namespace Digitalmes.Application.Mes.Traceability.Products.Queries;

/// <summary>
/// 产品过程追溯根据零件码模糊查询命令。
/// </summary>
/// <param name="Sn">零件码，模糊匹配</param>
/// <param name="Limit">显示的最多数量，默认20条。</param>
public sealed record GetProductLimitQuery(string Sn, int Limit = 20) : IRequest<IResult<List<ProdCurrentStateDto>>>;

internal sealed class GetProductLimitQueryHandler(IRepository<ProdCurrentState> currentStateRepo) : IRequestHandler<GetProductLimitQuery, IResult<List<ProdCurrentStateDto>>>
{
    public async Task<IResult<List<ProdCurrentStateDto>>> Handle(GetProductLimitQuery request, CancellationToken cancellationToken)
    {
        var (items, _) = await currentStateRepo.GetPagationAsync(m => m.Sn.Contains(request.Sn), 1, request.Limit, cancellationToken);
        return Result.From(items.Adapt<List<ProdCurrentStateDto>>());
    }
}
